/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nus.iss.t5.jass.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.text.ParseException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * This class represents for main Frame which take control of all the panels.
 * @author TMH : Create the Main Frame classes
 * @author Arunkumar : Added Button events for the AddJob Advertisemt
 * @author Eihan : Added Button events for the Delete Advertisemt
 * @author Noel : Added Button events for the Update Advertisemt
 */
public class MainFrame extends JFrame implements ActionListener {

    ViewPanel viewPanel;
    DeletePanel deletePanel;
    AddPanel addPanel;
    UpdatePanel updatePanel;

    public MainFrame() {
        // viewPanel = new ViewPanel();
        //setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        //getContentPane().add(viewPanel);
        //viewPanel.closeButton.addActionListener((ActionListener) this);
    }

    /*
     * This method set ViewPanel to listen on it.
     */
    public void setviewPanel(ViewPanel viewPanel) {
        this.viewPanel = viewPanel;
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        getContentPane().add(viewPanel);
        viewPanel.closeButton.addActionListener((ActionListener) this);
    }
    /*
     * This method set AddPanel to listen on it.
     */
     public void setaddPanel(AddPanel addPanel) {
        this.addPanel = addPanel;
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        getContentPane().add(addPanel);
        addPanel.addButton.addActionListener((ActionListener) this);
        addPanel.closeButton.addActionListener((ActionListener) this);
    }
     /*
     * This method set UpdatePanel to listen on it.
     */
     public void setUpdatePanel(UpdatePanel updatePanel)
     {
    	 this.updatePanel = updatePanel;
    	 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    	 getContentPane().add(updatePanel);
    	 updatePanel.saveButton.addActionListener((ActionListener)this);
    	 updatePanel.closeButton.addActionListener((ActionListener)this);
     }

     /*
     * This method set DeletePanel to listen on it.
     */
    public void setDeletePanel(DeletePanel deletePanel) {
        this.deletePanel = deletePanel;
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        getContentPane().add(deletePanel);
        deletePanel.deleteButton.addActionListener((ActionListener) this);
        deletePanel.closeButton.addActionListener((ActionListener) this);
    }

    public void windowClosed(WindowEvent e) {
        if (e.getSource() == viewPanel.closeButton) {
            setVisible(false);
            dispose();
        }
    }

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		
		if ((addPanel != null) && (e.getSource() == addPanel.closeButton))
		{
			 dispose();
		}
		// TODO Auto-generated method stub
		else if ((viewPanel != null) && (e.getSource() == viewPanel.closeButton)) {
            setVisible(false);
            dispose();
        }
		else if ((addPanel != null) && e.getSource() == addPanel.addButton) 
		{
	         System.out.println("Add");
	           //Should have the add function
	         try {
				addPanel.createAdvertisement();
	         }
	         catch (ParseException e1) {
				// TODO Auto-generated catch block
				//e1.printStackTrace();
				JOptionPane.showMessageDialog(this, "Invalid Data Entered");
	         }	
	         dispose();
	    }
		else if(updatePanel!=null && e.getSource() == updatePanel.saveButton)
		{
			updatePanel.saveAdvertisement();
			dispose();
		}
		else if(updatePanel!=null && e.getSource() == updatePanel.closeButton){
			setVisible(false);
			dispose();
		}
		else if ((deletePanel != null) && (e.getSource() == deletePanel.deleteButton)) {
			 System.out.println("Delete");
	           //Should have the add function
	        
				deletePanel.DeleteAdvertisement();        
	        
	         dispose();
        }
		else if ((deletePanel != null) && (e.getSource() == deletePanel.closeButton)) {
            
            dispose();
        }
	}

	
	
    /*public void actionPerformed(ActionEvent e) {
        if (e.getSource() == closeButton) {
            setVisible(false);
            dispose();
        }
    }*/
}
